#!/bin/bash

source config.sh

######字体颜色设置#########
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Yello_font_prefix="\033[33m" && Blue_font_prefix="\033[34m" && Pink_font_prefix="\033[35m"
######字体颜色设置#########

sh_ver=1.0


input_jdk=">>${Blue_background_prefix}是否安装jdk(n:取消,默认：n)：${Font_color_suffix}\c";

input_operate_type=">>${Blue_background_prefix}选择主菜单功能序号(默认为0)：${Font_color_suffix}\c";
input_init_menu=">>${Blue_background_prefix}是否重新加载主菜单？y/n(默认n)：${Font_color_suffix}\c";

printInfo(){
	 echo -e "${Green_font_prefix}========docker_tool$sh_ver---Powered By lvjinhui=====${Font_color_suffix}"
	 echo -e "${Red_font_prefix}-1: 退出${Font_color_suffix}"
	 echo -e "${Green_font_prefix}0 :${Font_color_suffix} ${Green_font_prefix}安装并启动tomcat容器${Font_color_suffix}"
	 echo -e "${Green_font_prefix}1 :${Font_color_suffix} ${Blue_font_prefix}安装并启动nginx容器${Font_color_suffix}"
	 echo -e "${Green_font_prefix}2 :${Font_color_suffix} ${Yello_font_prefix}安装并启动mysql${mysql_version}容器${Font_color_suffix}"
	 echo -e "${Green_font_prefix}3 :${Font_color_suffix} ${Green_font_prefix}安装并启动rabbitmq(带网页管理后台)容器${Font_color_suffix}"
	 echo -e "${Green_font_prefix}========docker_tool$sh_ver---Powered By lvjinhui=====${Font_color_suffix}"
}


#读取输入信息
inputInfo() {
	echo -e $1
	read -e temp
}

printInfo
 
inputInfo $input_operate_type;

operate_type=$temp;

if [ -z $operate_type ]; then
	operate_type=0
fi

while [ $operate_type -ne -1 ]

do
 #=============================================
 if [ $operate_type -eq 0 ]; then
	#确认安装
	inputInfo $input_jdk;
	input_jdk_flag=$temp;
	if [ -z $input_jdk_flag ];then
		input_jdk_flag="n"
	fi
	
	if [ $input_jdk_flag == "n" ]; then
		echo -e "${Green_font_prefix}取消安装jdk1.8 !!!${Font_color_suffix}"
	elif [ $input_jdk_flag == "y" ]; then
		echo -e "${Green_font_prefix}======    开始安装jdk    =====${Font_color_suffix}"
		yum -y install java-1.8.0-openjdk  java-1.8.0-openjdk-devel
		echo -e "${Green_font_prefix}===== jdk 版本  =====${Font_color_suffix}"
		java -version
		if [ $? -eq 0 ]; then
			echo -e "${Green_font_prefix}jdk安装成功!${Font_color_suffix}"
		else
			echo -e "${Red_font_prefix}jdk安装失败!${Font_color_suffix}"
		fi
	fi
	
	echo -e "${Green_font_prefix}拉取tomcat镜像...${Font_color_suffix}"
	
 	docker pull tomcat
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}出错！！！${Font_color_suffix}"
	fi
	echo -e "${Green_font_prefix}复制镜像到本机...${Font_color_suffix}"
	if [ ! -d ${tomcat_docker_copyurl} ]; then
	  mkdir -p ${tomcat_docker_copyurl}
	fi
	docker cp ${tomcat_docker_name}:/usr/local/tomcat  ${tomcat_docker_copyurl}
	echo -e "${Green_font_prefix}启动tomcat容器...${Font_color_suffix}"
	if [ ! -d ${tomcat_docker_weburl} ]; then
	  mkdir -p ${tomcat_docker_weburl}
	fi
	docker run --name ${tomcat_docker_name} -p ${tomcat_docker_port}:8080 -v ${tomcat_docker_weburl}:/usr/local/tomcat/webapps/ -d tomcat 
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}启动tomcat成功,端口:${tomcat_docker_port} 部署路径：${tomcat_docker_weburl} ！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}启动tomcat出错！！！${Font_color_suffix}"
	fi
 fi
#=============================================
 if [ $operate_type -eq 1 ]; then
	echo -e "${Green_font_prefix}拉取nginx镜像...${Font_color_suffix}"
 	docker pull nginx
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}出错！！！${Font_color_suffix}"
	fi
	echo -e "${Green_font_prefix}初始化nginx配置、页面、日志目录...${Font_color_suffix}"
	if [ ! -d ${nginx_conf} ]; then
	  mkdir -p ${nginx_conf}
	fi
	if [ ! -d ${nginx_www} ]; then
	  mkdir -p ${nginx_www}
	fi
	if [ ! -d ${nginx_log} ]; then
	  mkdir -p ${nginx_log}
	fi
	echo -e "${Green_font_prefix}nginx配置、页面、日志目录初始化完成...${Font_color_suffix}"
	echo -e "${Green_font_prefix}复制配置文件...${Font_color_suffix}"
	cp nginx.conf ${nginx_conf}/${nginx_conf_name}
	

	echo -e "${Green_font_prefix}启动nginx容器...${Font_color_suffix}"
	
	docker run -d -p ${nginx_port}:80 --name ${nginx_docker_name} -v ${nginx_www}:/etc/nginx/html -v ${nginx_conf}/${nginx_conf_name}:/etc/nginx/nginx.conf -v ${nginx_log}:/var/log/nginx nginx
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}启动nginx成功,端口:${nginx_port} 配置文件：${nginx_conf}/${nginx_conf_name} ！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}启动nginx出错！！！${Font_color_suffix}"
	fi
 fi
 
 #=============================================
 if [ $operate_type -eq 2 ]; then
	echo -e "${Green_font_prefix}拉取mysql镜像...${Font_color_suffix}"
 	docker pull mysql:${mysql_version}
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}出错！！！${Font_color_suffix}"
	fi
	echo -e "${Green_font_prefix}初始化mysql配置、数据、日志目录...${Font_color_suffix}"
	if [ ! -d ${mysql_conf} ]; then
	  mkdir -p ${mysql_conf}
	fi
	if [ ! -d ${mysql_data} ]; then
	  mkdir -p ${mysql_data}
	fi
	if [ ! -d ${mysql_logs} ]; then
	  mkdir -p ${mysql_logs}
	fi
	echo -e "${Green_font_prefix}mysql配置、数据、日志目录初始化完成...${Font_color_suffix}"

	echo -e "${Green_font_prefix}启动mysql${mysql_version}容器...${Font_color_suffix}"

	docker run -p ${mysql_port}:3306 --name ${mysql_docker_name} -v ${mysql_conf}:/etc/mysql/conf.d -v ${mysql_logs}:/logs -v ${mysql_data}:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=${mysql_root_pwd} -d mysql:${mysql_version}
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}启动mysql成功,端口:${mysql_port} 配置：${mysql_conf} 数据路径：${mysql_data} ！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}启动mysql出错！！！${Font_color_suffix}"
	fi
 fi
 
 #=============================================
 if [ $operate_type -eq 3 ]; then
	echo -e "${Green_font_prefix}拉取rabbitmq镜像...${Font_color_suffix}"
 	docker pull rabbitmq:management
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}出错！！！${Font_color_suffix}"
	fi
	echo -e "${Green_font_prefix}初始化rabbitmq数据目录...${Font_color_suffix}"
	if [ ! -d ${rabbitmq_data} ]; then
	  mkdir -p ${rabbitmq_data}
	fi
	echo -e "${Green_font_prefix}rabbitmq数据目录初始化完成...${Font_color_suffix}"

	echo -e "${Green_font_prefix}启动rabbitmq容器...${Font_color_suffix}"

	docker run -d --name ${rabbitmq_docker_name} -p ${rabbitmq_port_send}:5672 -p ${rabbitmq_port_manage}:15672 -v ${rabbitmq_data}:/var/lib/rabbitmq --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost  -e RABBITMQ_DEFAULT_USER=${rabbitmq_user} -e RABBITMQ_DEFAULT_PASS=${rabbitmq_pwd} rabbitmq:management 
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}启动rabbitmq成功,通信端口:${rabbitmq_port_send} 后台端口：${rabbitmq_port_manage}  数据路径：${rabbitmq_data} ！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}启动rabbitmq出错！！！${Font_color_suffix}"
	fi
 fi

############################################分割线#########################################################
 
 inputInfo $input_init_menu;
 init_menu=$temp;
 if [ -z $init_menu ];then
	init_menu="n"
 fi
 if [ $init_menu == "y" ]; then
	#重新显示菜单
	printInfo
 fi
 
	
 inputInfo $input_operate_type;
 operate_type=$temp;
 if [ -z $operate_type ]; then
	operate_type=0
 fi
 
done